package userInterface;
import dataProces.DatabaseOperations;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Login extends JDialog implements ActionListener {
    public static String user= "";
    String[] prompt={"账号/手机号/身份证","密码","注册","登录","退出","登录界面"};
    JButton[] jButtons=new JButton[3];
    JPanel[] jPanels=new JPanel[3];
    JTextField jTextField=new JTextField(25);
    JPasswordField jField=new JPasswordField(15);
    public Login() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(3, 1));
        for(int i=0;i<jPanels.length;i++){
            jPanels[i]=new JPanel();
            if(i<2){
                jPanels[i].add(new JLabel(prompt[i]));
                if(i==0)jPanels[i].add(jTextField);
                else jPanels[i].add(jField);
            }else
                for(int j=0;j<jButtons.length;j++){
                    jButtons[j]=new JButton(prompt[i+j]);
                    jButtons[j].addActionListener(this);
                    jPanels[i].add(jButtons[j]);
                }
            container.add(jPanels[i]);
        }
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 300);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
        setTitle(prompt[5]);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==jButtons[0]){
            dispose();
            new signUp();
        }else if(e.getSource()==jButtons[1]){
            DatabaseOperations dOperations=new DatabaseOperations();
            String account=jTextField.getText();
            String password=new String(jField.getPassword());
            if(dOperations.dataQueries("user",account,"password")==null)
                JOptionPane.showMessageDialog(null, "查无此号！");
            else if(dOperations.dataQueries("user",account,"password").equals(password)){
                user=account;
                JOptionPane.showMessageDialog(null, "欢迎您，尊敬的"+dOperations.dataQueries("user",user, "name"));
                dispose();
                new UserMenu();
            }
            else {
                JOptionPane.showMessageDialog(null, "密码错误！");
                dispose();
                new Login();
            }

        }else if(e.getSource()==jButtons[2]){
            dispose();
        }
    }
    public static void main(String[] args) {
        new Login();
    }
}
